package com.simdo.g768.Util;

import java.util.ArrayList;
import java.util.List;

public class Battery {

	private final List<batteryArray> batteryList = new ArrayList<>();
	private final List<Integer> volList = new ArrayList<>();

	public String percent;

	private static Battery instance;

	public static Battery build() {
		synchronized (Battery.class) {
			if (instance == null) {
				instance = new Battery();
			}
		}
		return instance;
	}

	public Battery() {
		init();
	}

	private void init() {
		percent = "检测中";
		volList.clear();
		batteryList.clear();
		// 实际测试做调整
		batteryList.add(new batteryArray(50000, "100")); 	// 第一个值默认给大点，无所谓
		batteryList.add(new batteryArray(25250, "100"));
		batteryList.add(new batteryArray(25120, "100"));
		batteryList.add(new batteryArray(25091, "100"));

		batteryList.add(new batteryArray(25090, "99"));
		batteryList.add(new batteryArray(25075, "99"));
		batteryList.add(new batteryArray(25000, "99"));

		// 19个
		batteryList.add(new batteryArray(24950, "98"));
		batteryList.add(new batteryArray(24850, "97"));
		batteryList.add(new batteryArray(24750, "96"));
		batteryList.add(new batteryArray(24650, "95"));
		batteryList.add(new batteryArray(24550, "94"));
		batteryList.add(new batteryArray(24450, "93"));
		batteryList.add(new batteryArray(24350, "92"));
		batteryList.add(new batteryArray(24250, "91"));
		batteryList.add(new batteryArray(24150, "90"));
		batteryList.add(new batteryArray(24050, "89"));
		batteryList.add(new batteryArray(23950, "88"));
		batteryList.add(new batteryArray(23850, "87"));
		batteryList.add(new batteryArray(23750, "86"));
		batteryList.add(new batteryArray(23650, "85"));
		batteryList.add(new batteryArray(23550, "84"));
		batteryList.add(new batteryArray(23450, "83"));
		batteryList.add(new batteryArray(23350, "82"));
		batteryList.add(new batteryArray(23250, "81"));
		batteryList.add(new batteryArray(23150, "80"));

		// 25个
		batteryList.add(new batteryArray(23050, "79"));
		batteryList.add(new batteryArray(22950, "78"));
		batteryList.add(new batteryArray(22850, "77"));
		batteryList.add(new batteryArray(22750, "76"));
		batteryList.add(new batteryArray(22650, "75"));
		batteryList.add(new batteryArray(22550, "74"));
		batteryList.add(new batteryArray(22450, "73"));
		batteryList.add(new batteryArray(22350, "72"));
		batteryList.add(new batteryArray(22250, "71"));
		batteryList.add(new batteryArray(22150, "70"));
		batteryList.add(new batteryArray(22050, "69"));
		batteryList.add(new batteryArray(21950, "68"));

		batteryList.add(new batteryArray(21870, "67"));
		batteryList.add(new batteryArray(21790, "66"));
		batteryList.add(new batteryArray(21710, "65"));
		batteryList.add(new batteryArray(21630, "64"));
		batteryList.add(new batteryArray(21550, "63"));
		batteryList.add(new batteryArray(21470, "62"));
		batteryList.add(new batteryArray(21390, "61"));
		batteryList.add(new batteryArray(21310, "60"));
		batteryList.add(new batteryArray(21230, "59"));
		batteryList.add(new batteryArray(21150, "58"));
		batteryList.add(new batteryArray(21070, "57"));
		batteryList.add(new batteryArray(20990, "56"));
		batteryList.add(new batteryArray(20910, "55"));
		batteryList.add(new batteryArray(20830, "54"));
		batteryList.add(new batteryArray(20750, "53"));
		batteryList.add(new batteryArray(20670, "52"));
		batteryList.add(new batteryArray(20590, "51"));
		batteryList.add(new batteryArray(20510, "50"));

		// 29个
		batteryList.add(new batteryArray(20450, "49"));
		batteryList.add(new batteryArray(20390, "48"));
		batteryList.add(new batteryArray(20330, "47"));
		batteryList.add(new batteryArray(20270, "46"));
		batteryList.add(new batteryArray(20210, "45"));
		batteryList.add(new batteryArray(20150, "43"));
		batteryList.add(new batteryArray(20100, "42"));
		batteryList.add(new batteryArray(20050, "41"));
		batteryList.add(new batteryArray(20000, "40"));
		batteryList.add(new batteryArray(19950, "39"));
		batteryList.add(new batteryArray(19900, "38"));
		batteryList.add(new batteryArray(19850, "37"));
		batteryList.add(new batteryArray(19800, "36"));
		batteryList.add(new batteryArray(19750, "35"));
		batteryList.add(new batteryArray(19700, "34"));
		batteryList.add(new batteryArray(19650, "33"));
		batteryList.add(new batteryArray(19600, "32"));
		batteryList.add(new batteryArray(19550, "31"));
		batteryList.add(new batteryArray(19500, "30"));
		batteryList.add(new batteryArray(19450, "29"));
		batteryList.add(new batteryArray(19400, "28"));
		batteryList.add(new batteryArray(19350, "27"));
		batteryList.add(new batteryArray(19300, "26"));
		batteryList.add(new batteryArray(19250, "25"));
		batteryList.add(new batteryArray(19200, "24"));
		batteryList.add(new batteryArray(19150, "23"));
		batteryList.add(new batteryArray(19100, "22"));
		batteryList.add(new batteryArray(19050, "21"));
		batteryList.add(new batteryArray(19000, "20"));

		// 40个
		batteryList.add(new batteryArray(18950, "19"));
		batteryList.add(new batteryArray(18850, "18"));
		batteryList.add(new batteryArray(18750, "17"));
		batteryList.add(new batteryArray(18650, "16"));
		batteryList.add(new batteryArray(18550, "15"));
		batteryList.add(new batteryArray(18450, "14"));
		batteryList.add(new batteryArray(18350, "13"));
		batteryList.add(new batteryArray(18250, "12"));
		batteryList.add(new batteryArray(18150, "11"));
		batteryList.add(new batteryArray(18000, "10"));

		batteryList.add(new batteryArray(17850, "9"));
		batteryList.add(new batteryArray(17750, "8"));
		batteryList.add(new batteryArray(17650, "7"));
		batteryList.add(new batteryArray(17550, "5"));
		batteryList.add(new batteryArray(17450, "4"));
		batteryList.add(new batteryArray(17350, "3"));
		batteryList.add(new batteryArray(17250, "2"));
		batteryList.add(new batteryArray(17200, "2"));
		batteryList.add(new batteryArray(17150, "1"));
		batteryList.add(new batteryArray(17100, "1"));
		batteryList.add(new batteryArray(17000, "0"));
	}

	public void handleVol(int vol) {
		// 小于17V，电池就会断电保护，此处避免出现极端用1000处理
		if (vol < 17000) vol = 17000;

		volList.add(vol);
		int size = volList.size();
		if (size > 5){
			if (size == 6){
				// 一开始的数据不稳定，在此过滤一次，替换前2个
				volList.set(0, vol);
				volList.set(1, vol);
			}
			if (size > 10) volList.remove(0); // 最多取10次数据做处理

			int sum = 0;
			int max = volList.get(0);
			int min = volList.get(0);
			for (int i = 0; i < volList.size(); i++) {
				sum += volList.get(i);
				if (max < volList.get(i)) max = volList.get(i);
				if (min > volList.get(i)) min = volList.get(i);
			}
			// 减去一个最大值、最小值，再取平均值
			sum -= max;
			sum -= min;
			int battery = sum / (volList.size() - 2);
			//battery = battery * 315 / 120; // 分压
			// 取百分比
			for (int i = 0; i < batteryList.size() - 1; i++) {
				if (battery < batteryList.get(i).vol && battery >= batteryList.get(i + 1).vol) {
					percent = batteryList.get(i + 1).getPercent();
					break;
				}
			}
		}
	}

	public String getPercent() {
		return percent;
	}

	static class batteryArray {
		int vol;
		String percent;

		public batteryArray(int vol, String percent) {
			super();
			this.vol = vol;
			this.percent = percent;
		}

		public int getVol() {
			return vol;
		}

		public String getPercent() {
			return percent;
		}
	}
}
